package com.ruoyi.inspection.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.constant.AmapApiConstants;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.http.HttpClientUtil;
import com.ruoyi.inspection.service.LocationService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 地理位置Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-14
 */
@Service
public class LocationServiceImpl implements LocationService {

    @Override
    public String getLongitudeLatitude(String address) {
        String url = AmapApiConstants.url_geocode;
        Map<String, Object> params = new HashMap<>();
        params.put("address", address);
        params.put("key", "5d1a6dfdcc1f53742e10404e57b4e39e");
        String result = HttpClientUtil.doGet(url, params, Constants.UTF8);
        JSONObject resultJson = JSONObject.parseObject(result);
        JSONArray arr = resultJson.getJSONArray("geocodes");
        if (CollectionUtils.isEmpty(arr)) {
            return null;
        }
        JSONObject json = (JSONObject) arr.get(0);
        String location = json.getString("location");
        return location;
    }
}
